package com.zdy.showgoods;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.example.networklibrary.BaseObserver;
import com.example.networklibrary.MD5;
import com.example.networklibrary.RxHttp;
import com.example.networklibrary.SharedPreferencesUtils;
import com.example.networklibrary.ToastUtils;
import com.example.networklibrary.config.KeyInterface;
import com.example.networklibrary.entity.RequestEntity;
import com.example.networklibrary.model.GuestModel;
import com.zdy.modellibrary.base.BaseActivity;
import com.zdy.utils.CheckUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ActivityRegister extends BaseActivity implements View.OnClickListener {

    private ImageView guest_register_back;
    private TextView guest_register_phone;
    private EditText guest_register_name;
    private EditText guest_register_age;
    private RadioButton guest_register_man;
    private RadioButton guest_register_woman;
    private Button guest_register_confirm;
    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        phone = this.getIntent().getExtras().getString("phone");
        guest_register_back = findViewById(R.id.guest_register_back);
        guest_register_phone = findViewById(R.id.guest_register_phone);
        guest_register_name = findViewById(R.id.guest_register_name);
        guest_register_woman = findViewById(R.id.guest_register_woman);
        guest_register_man = findViewById(R.id.guest_register_man);
        guest_register_age = findViewById(R.id.guest_register_age);
        guest_register_confirm = findViewById(R.id.guest_register_confirm);
        guest_register_confirm.setOnClickListener(this);
        guest_register_back.setOnClickListener(this);
        guest_register_phone.setText(phone);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.guest_register_confirm) {
            toRegister();
        } else if (v.getId() == R.id.guest_register_back) {
            finish();
        }
    }

    private void toRegister() {
        if (TextUtils.isEmpty(guest_register_name.getText().toString())) {
            Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show();
            return;
        }
        if (guest_register_woman.isChecked() && !guest_register_man.isChecked()) {
            Toast.makeText(this, "请输入选择性别", Toast.LENGTH_SHORT).show();
        }
        RequestEntity requestEntity = new RequestEntity();
        JSONObject request = new JSONObject();
        request.put("customer_name", guest_register_name.getText().toString());
        request.put("sex", guest_register_man.isChecked() ? 1 : 0);
        request.put("age", TextUtils.isEmpty(guest_register_age.getText().toString()) ? 0 : guest_register_age.getText().toString());
        request.put("phone_number", guest_register_phone.getText().toString());
        requestEntity.setBody(request);
        JSONObject header = new JSONObject();
        header.put("request_seq", KeyInterface.REQUEST_SEQ);
        header.put("sign", "");
        header.put("sys_id", KeyInterface.SYS_ID);
        header.put("tra_code", KeyInterface.GUEST_REGISTER);
        requestEntity.setHeader(header);

        try {
            String signString = JSONObject.toJSONString(requestEntity);
            String sign = MD5.getSign(signString);
            header.put("sign", sign);
            requestEntity.setHeader(header);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String json = "";
        try {
            json = URLEncoder.encode(JSONObject.toJSONString(requestEntity), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        RxHttp.getInstance()
                .getSyncServer()
                .guestLogin(SharedPreferencesUtils.getString(this, KeyInterface.AppURL), json)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<GuestModel>() {
                    @Override
                    public void onSuccess(GuestModel result) {
                        try {
                            SharedPreferencesUtils.setString(ActivityRegister.this, KeyInterface.GuestInfo, JSONObject.toJSONString(result));
                            toMain();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        ToastUtils.showShort("注册失败");
                    }
                });
    }

    private void toMain() {
        finish();
    }
}
